package com.itboye.mengwu.app.ui.aizhiwu;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.inageview.ItemEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.MyBoyAdapter;
import com.itboye.mengwu.data.model.AllData;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MyTieZiCenterBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiModel;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.luntan.comment.adapter.CommentAdapter;
import com.itboye.mengwu.luntan.comment.adapter.CommentReplyAdapter;
import com.itboye.mengwu.luntan.comment.bean.Reply;

//import com.itboye.core.imageview.RoundImage;

public class GeRenKongJianActivity extends BaseActivity implements Observer {

	private Button btn_comment, btn_reply;
	private EditText edt_reply;

	private CommentAdapter commentAdapter;
	private CommentReplyAdapter commentReplyAdapter;

	private static final int ONE_COMMENT_CODE = -1;

	private ArrayList<TieZiModel.GetAllList> commentList;
	private List<Reply> replyList;

	/** Item数据实体集合 */
	private ArrayList<ItemEntity> itemEntities;
	/** ListView对象 */
	private ListView listview;
	// private RoundImage image;
	String imageUrl = "http://hiphotos.baidu.com/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg";
	private TextView textGuan;
	private ImageView back;
	TieZiPresenter presenter;
	OnClickListener dianzan;
	ArrayList<TieZiModel.GetAllList.UserInfo> arrayListUserInfo = new ArrayList<TieZiModel.GetAllList.UserInfo>();
	private MyTieZiCenterPresenter lunTanPresenter;
	private ArrayList<TieZiBaoHanReply.GetAList> list;
	private MyBoyAdapter adapter;
	private TieZiBaoHanReply bean;
	View headerView;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.gerenkongjian;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		headerView = LayoutInflater.from(this).inflate(
				R.layout.item_hederlistview, null);
		initCommentData();
		// listview.setAdapter(new ListItemAdapter(getActivity(),
		// itemEntities));
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

		}
	};

	private void initCommentData() {
		list = new ArrayList<TieZiBaoHanReply.GetAList>();
		adapter = new MyBoyAdapter(list, this, clickListener);
		listview.addHeaderView(headerView);
		listview.setAdapter(adapter);
		lunTanPresenter = new MyTieZiCenterPresenter(this);
		commentList = new ArrayList<TieZiModel.GetAllList>();
		LuntanTieZiBean model = new LuntanTieZiBean();
		model.setUid(SPUtils.get(this, null, SpPublicParams.UID, "") + "");
		lunTanPresenter.getInfo(model);

		// TieZiModel comment = new TieZiModel();
		// setUsername("小花");
		// comment.setContent("我是一楼");
		// commentList.add(comment);
		//
		// Comment comment2 = new Comment();
		// comment2.setUsername("阿黄");
		// comment2.setContent("我是二楼");
		// commentList.add(comment2);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				// if (position != 0) {
				Intent intent = new Intent(GeRenKongJianActivity.this,
						ActivityQueryWodeDongTaiDetail.class);
				startActivity(intent);
				// }else {
				// txt_title
				// }

			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			// model = (TieZiModel) entity.getData();
			// for (int i = 0; i < model.getList().size(); i++) {
			// // arrayListUserInfo=
			// arrayListUserInfo.add(model.getList().get(i).getUserInfo());
			// }
			// commentList.addAll(model.getList());
			// commentReplyAdapter = null;
			// commentAdapter = new CommentAdapter(GeRenKongJianActivity.this,
			// commentList,
			// replyToCommentListener, commentReplyAdapter,
			// replyToReplyListener,dianzan);
			// listview.setAdapter(commentAdapter);
			bean = (TieZiBaoHanReply) entity.getData();
			list.addAll(bean.getList());
			adapter.notifyDataSetChanged();
		}
	}

	/**
	 * 发表评论的监听
	 */
	private OnClickListener addCommentListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onCreateDialog(ONE_COMMENT_CODE, ONE_COMMENT_CODE);
		}
	};

	/**
	 * 回复评论的监听（回复楼主）
	 */
	private OnClickListener replyToCommentListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int position = (Integer) v.getTag();
			onCreateDialog(ONE_COMMENT_CODE, position);
		}
	};

	/**
	 * 互相回复的监听（楼中楼）
	 */
	private OnClickListener replyToReplyListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int parentPosition = (Integer) v.getTag(R.id.tag_first);
			int childPosition = (Integer) v.getTag(R.id.tag_second);
			onCreateDialog(parentPosition, childPosition);
		}
	};
	private TieZiModel model;

	/**
	 * 弹出评论的对话框
	 * 
	 * @param parentPositon
	 *            父节点的position
	 * @param childPostion
	 *            子节点的position
	 * @return
	 */
	protected Dialog onCreateDialog(final int parentPositon,
			final int childPostion) {
		final Dialog customDialog = new Dialog(GeRenKongJianActivity.this);
		LayoutInflater inflater = getLayoutInflater();
		View mView = inflater.inflate(R.layout.dialog_comment, null);
		edt_reply = (EditText) mView.findViewById(R.id.edt_comments);
		btn_reply = (Button) mView.findViewById(R.id.btn_send);

		customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		customDialog.setContentView(mView);
		customDialog.show();

		btn_reply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (childPostion) {
				case ONE_COMMENT_CODE:
					if (TextUtils.isEmpty(edt_reply.getText().toString())) {
						Toast.makeText(GeRenKongJianActivity.this, "内容不能为空",
								Toast.LENGTH_SHORT).show();
					} else {
						TieZiModel.GetAllList comment = new TieZiModel.GetAllList();
						// comment.setUsername("海盗");
						// comment.setContent(edt_reply.getText().toString());

						// commentList.add(comment);
						// commentAdapter.clearList();
						// commentAdapter.updateList(commentList);
						// commentAdapter.notifyDataSetChanged();
						// customDialog.dismiss();
						// edt_reply.setText("");
					}
					break;
				default:
					if (TextUtils.isEmpty(edt_reply.getText().toString())) {
						Toast.makeText(GeRenKongJianActivity.this, "内容不能为空",
								Toast.LENGTH_SHORT).show();
					} else {
						Reply reply = new Reply();
						reply.setUsername("" + parentPositon + childPostion);
						reply.setContent(edt_reply.getText().toString());
						//
						// if (parentPositon != -1) {
						// reply.setReplyTo(commentList.get(parentPositon)
						// .getReplyList().get(childPostion)
						// .getUsername());
						// commentList.get(parentPositon).getReplyList()
						// .add(reply);
						// } else {
						// replyList = commentList.get(childPostion)
						// .getReplyList();
						// replyList.add(reply);
						// commentList.get(childPostion).setReplyList(
						// replyList);
						// }

						// commentAdapter.clearList();
						// commentAdapter.updateList(commentList);
						// commentAdapter.notifyDataSetChanged();
						// customDialog.dismiss();
						// edt_reply.setText("");
					}
					break;
				}
			}
		});
		return customDialog;
	}

}
